package com.jboost.modules.app.constant;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

public interface BaseEnumInterface<T> {
    T getType();

    String getDesc();

    default BaseEnumInterface[] valueArray() throws Exception {
        Class<?> clazz = Class.forName(this.getClass().getName());
        Method staticMethod = clazz.getMethod("values");
        //获取所有枚举实例
        Enum[] enumConstants = (Enum[]) clazz.getEnumConstants();
        Object invoke = staticMethod.invoke(enumConstants[0]);
        return (BaseEnumInterface[]) invoke;
    };

    default String getDescByType(String type) {
        Optional<BaseEnumInterface> first = null;
        try {
            first = Arrays.stream(valueArray()).filter(e -> {
                return Objects.equals(type, e.getType());
            }).findFirst();
        } catch (Exception e) {
            return null;
        }
        if (!first.isPresent()) {
            return null;
        }
        BaseEnumInterface baseInterface = (first.get());
        if (Objects.nonNull(baseInterface)) {
            return baseInterface.getDesc();
        }
        return null;
    }

}
